package PKA;

import MAgentAssignment.ontology.FlightVocabulary;
import MAgentAssignment.ontology.actions.CreateAirpot;
import PKA.msg.FullMsg;
import PKA.msg.MsgFactory;
import jade.content.lang.Codec;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

import java.io.IOException;
import java.security.Security;

/**
 * Created by IntelliJ IDEA.
 * User: prageeth
 * Date: 4/14/12
 * Time: 10:29 PM
 * To change this template use File | Settings | File Templates.
 */
public class PKAAgent extends Agent
{
    private Codec codec = new SLCodec();
    //    private Ontology ontology = FlightOntology.getInstance();
    private AID[] monitorAgents;

    @Override
    protected void setup()
    {

        getContentManager().registerLanguage( codec );
        //        getContentManager().registerOntology(ontology);
        //        monitorAgents = new AID[]{new AID(InterfaceAgent.agentName, AID.ISLOCALNAME)};
        ServiceDescription sd = new ServiceDescription();
        sd.setType( "PKAAgent" );
        sd.setName( getLocalName() );
        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName( getAID() );
        dfd.addServices( sd );

        try
        {
            DFService.register( this, dfd );
        }
        catch( FIPAException fe )
        {
            fe.printStackTrace();
        }

        addBehaviour( new CyclicBehaviour()
        {
            @Override
            public void action()
            {
                ACLMessage msg = null;
                // handle the key request and respond back
                msg = receive( MessageTemplate.MatchPerformative( ACLMessage.REQUEST ) );
                if( msg != null && msg.getContent() != null && msg.getContent().equals( PKA.SecurityVocabulary.PK_REQUEST ) )
                {
                    ACLMessage aclMessage = msg.createReply();
                    aclMessage.setPerformative( ACLMessage.INFORM );

                    FullMsg fullMsg = MsgFactory.getMessage( null, null, null, null );
                    try
                    {
                        aclMessage.setContentObject( fullMsg );
                    }
                    catch( IOException e )
                    {
                        e.printStackTrace();
                    }
                    send( aclMessage );
                }
            }
        } );
    }
}
